using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using StructureMap;
using Ebc.CustomSerializer.Contracts;

namespace Ebc.DesignerDsl
{
    public class BootStrapper : ServiceLocatorImplBase
    {
        private readonly IContainer _container;

        private static BootStrapper _instance;

        public static BootStrapper Instance
        {
            get
            {
                return _instance ?? (_instance = new BootStrapper());
            }
        }

        public BootStrapper()
        {
            _container = new Container(c => c.Scan(scanner =>
                                                          {
                                                              scanner.AssemblyContainingType(typeof(IEbcBoardSerializer));                                                              
                                                              scanner.WithDefaultConventions();
                                                              scanner.Convention<SerializerConvention>();
                                                          }));
        }

        public IContainer Container
        {
            get { return _container; }
        }

        protected override object DoGetInstance(Type serviceType, string key)
        {
            return string.IsNullOrEmpty(key)
                    ? _container.GetInstance(serviceType)
                    : _container.GetInstance(serviceType ?? typeof(object), key);
        }

        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>();
        }
    }
}